/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.dwusoft.sendmsg.service.plugin  
 */
package com.dwusoft.sendmsg.service.plugin;

import java.util.HashMap;
import java.util.Map;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.database.constant.SystemTools;
import com.apache.database.constant.Validator;
import com.apache.tools.StrUtil;
import com.dwusoft.sendmsg.common.SendToolsUtil;
import com.dwusoft.sendmsg.common.entity.Sendmsg;
import com.dwusoft.sendmsg.manager.SendmsgManager;
import com.dwusoft.sendmsg.service.impl.SendMobileServiceImpl;
import com.dwusoft.sendmsg.service.template.TemplateHelper;

/**
 * description:  接收消息
 * @author Hou Dayu 创建时间：2016-5-18  
 * @Copyright(c)2014:北京东吴软件技术有限公司
 */
public class SaveSendmsgPluginImpl implements PluginConnector {

	private SendmsgManager sendmsgManager;

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.dwusoft.api.manager.PluginConnector#execute(com.dwusoft.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo param) throws Exception {
		Sendmsg msg = new Sendmsg();
		msg = (Sendmsg) SendToolsUtil.getInstance().convert2Bean(param.getParams(), msg);
		ResultEntity result = new ResultEntity();
		if ("email".equalsIgnoreCase(msg.getMsgType())) {
			if (Validator.isNull(msg.getToUserEmail()) || Validator.isNull(msg.getMsgTitle())
					|| Validator.isNull(msg.getMsgContent())) {
				result.setResult("false");
				result.setMessage("缺少必要参数");
				return result;
			}
			msg.setFromUserEmail(StrUtil.doNull(msg.getFromUserEmail(), SystemTools.getInstance().getValue("mail_user")));
			msg.setMailHost(StrUtil.doNull(msg.getMailHost(), SystemTools.getInstance().getValue("mail_host")));
			msg.setMailProtocol(StrUtil.doNull(msg.getMailProtocol(),
					SystemTools.getInstance().getValue("mail.protocol")));
			msg.setUserPass(StrUtil.doNull(msg.getUserPass(), SystemTools.getInstance().getValue("mail_user")));
		} else if ("mobile".equalsIgnoreCase(msg.getMsgType())) {
			if (Validator.isNull(msg.getToUserMobile())) {
				result.setResult("false");
				result.setMessage("缺少必要参数");
				return result;
			}
//			msg.setFromUserMobile(StrUtil.doNull(msg.getFromUserMobile(),
//					SystemTools.getInstance().getValue("mobile_user")));
		} else if ("all".equalsIgnoreCase(msg.getMsgType())) {
			if (Validator.isNull(msg.getToUserEmail()) || Validator.isNull(msg.getMsgTitle())
					|| Validator.isNull(msg.getMsgContent()) || Validator.isNull(msg.getToUserMobile())) {
				result.setResult("false");
				result.setMessage("缺少必要参数");
				return result;
			}
			msg.setFromUserEmail(StrUtil.doNull(msg.getFromUserEmail(), SystemTools.getInstance().getValue("mail_user")));
			msg.setMailHost(StrUtil.doNull(msg.getMailHost(), SystemTools.getInstance().getValue("mail_host")));
			msg.setMailProtocol(StrUtil.doNull(msg.getMailProtocol(),
					SystemTools.getInstance().getValue("mail.protocol")));
			msg.setUserPass(StrUtil.doNull(msg.getUserPass(), SystemTools.getInstance().getValue("mail_user")));
			msg.setFromUserMobile(StrUtil.doNull(msg.getFromUserMobile(),
					SystemTools.getInstance().getValue("mobile_user")));
		} else {
			result.setResult("false");
			result.setMessage("待发布的消息类型不匹配");
			return result;
		}
		long num = 1;
		ParamsVo<Sendmsg> vo = new ParamsVo<Sendmsg>();
		msg.setMsgStatus("1");//待发送
		//判断是否实时发送--》短信
		if(msg.getMsgType().equalsIgnoreCase("mobile")&&msg.getIsSendNow().equals("1")){
			num = sendMobile(msg);
			if (num < 1) {
				msg.setMsgStatus("3");//发送失败
				result.setResult("F");
				result.setMessage(msg.getMsgTitle() + "发送失败");
			}else{
				msg.setMsgStatus("2");//发送成功
			}
		}
		vo.setObj(msg);
		String id = sendmsgManager.saveInfo(vo);
		if (Validator.isNull(id)) {
			result.setResult("F");
			result.setMessage(msg.getMsgTitle() + "消息发布失败");
		} else {
			result.setResult("T");
			result.setEntity(id);
			result.setMessage(msg.getMsgTitle() + "消息发布成功");
		}
		return result;
	}
	private long sendMobile(Sendmsg sendMsg) {
		long mark = 0;
		String mobile = sendMsg.getToUserMobile();
		if (Validator.isNull(mobile))
			return 1;
		Map<String, String> params = new HashMap<String, String>();
		boolean flag = TemplateHelper.instance().getSends("mobile").sendMessage(sendMsg, params);
		if (!flag)
			return 1;
		return mark;
	}
	public void setSendmsgManager(SendmsgManager sendmsgManager) {
		this.sendmsgManager = sendmsgManager;
	}
}
